//
//  PreHeadView.m
//  Weibbo
//
//  Created by 李 林刚 on 13-6-30.
//  Copyright (c) 2013年 Omnivores. All rights reserved.
//

#import "PreHeadView.h"

@implementation PreHeadView
@synthesize delegate;

-(void)dealloc
{
    UIRelease(backGroundImageView);
    UIRelease(titleTextView);
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColorRGB(44, 44, 44);
        backGroundImageView = [[UIImageView alloc]initWithFrame:frame];
        backGroundImageView.image = [UIImage imageNamed:@"pre_head_bg.png"];
        [self addSubview:backGroundImageView];
        
        titleTextView = [[UITextView alloc]initWithFrame:CGRectMake(10,134, 310, 92)];
        [titleTextView setKeyboardAppearance:UIKeyboardAppearanceAlert];
        titleTextView.font = [UIFont boldSystemFontOfSize:24];
        titleTextView.textColor = UIWhiteColor;
        titleTextView.delegate = self;
        [titleTextView setReturnKeyType:UIReturnKeyDone];
        titleTextView.textAlignment = NSTextAlignmentLeft;
        titleTextView.showsVerticalScrollIndicator = NO;
        titleTextView.backgroundColor = UIClearColor;
        [self addSubview:titleTextView];
    }
    return self;
}

- (void)setDraftEntity:(DraftEntity *)entity
{
    [titleTextView setText:entity.titleText];
    [backGroundImageView setImage:[UIImage imageWithName:entity.titleImageName]];
}

#pragma mark - UITextViewDelegate method

-(void)textViewDidChange:(UITextView *)textView{
    if (textView.contentSize.height > 90) {
        textView.text = [textView.text substringToIndex:[[textView text] length] - 1];
    }
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [titleTextView resignFirstResponder];
        return NO;
    }
    return YES;
}

-(void)textViewDidEndEditing:(UITextView *)textView
{
    [delegate PreHeadViewDelegateUpDateTitleText:textView.text];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
